<template>
  <div>
    <div v-if="staticshow">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <el-select
            v-model="cForm.gradeid"
            :placeholder="getreqgetclassdata[0].title"
            clearable
            filterable
            @change="getgradeid"
          >
            <el-option
              v-for="item in getreqgetclassdata"
              :key="item.list2_leftid"
              :label="item.title"
              :value="item.list2_leftid"
            >
            </el-option>
          </el-select>
          <el-button
            style="
              display: flex;
              justify-content: center;
              background-color: #9c446e;
              color: #fff;
            "
            size="default"
            @click="addclassdata"
            >添加</el-button
          >
        </div>
      </el-card>

      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <el-table :data="classdata" border stripe>
          <el-table-column type="index" label="课程编号" :width="80">
          </el-table-column>
          <el-table-column prop="" label="课程" width="width">
            <template slot-scope="{ row, $index }">
              <div>
                {{ row.title }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="" label="封面" width="width">
            <template slot-scope="{ row, $index }">
              <img
                :src="baseurl + row.imgrul"
                alt=""
                style="width: 200px; height: 200px"
              />
            </template>
          </el-table-column>
          <el-table-column prop="" label="视频链接" width="width">
            <template slot-scope="{ row, $index }">
              <div>
                {{ row.videourl }}
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <div>
                <el-button
                  type="success"
                  size="mini"
                  icon="el-icon-sort-up"
                  @click="updatashop(row)"
                  circle
                  v-if="row.ishow"
                ></el-button>
                <el-button
                  type="info"
                  size="mini"
                  icon="el-icon-sort-down"
                  @click="updatashop(row)"
                  v-else
                  circle
                ></el-button>
                <el-button
                  type="warning"
                  size="mini"
                  circle
                  icon="el-icon-edit"
                  @click="showshopdetail(row)"
                ></el-button>

                <el-button
                  type="primary"
                  size="mini"
                  circle
                  icon="el-icon-info"
                  @click="maintenance(row)"
                ></el-button>
                <el-button
                  type="danger"
                  size="mini"
                  circle
                  @click="deletebutton(row)"
                  icon="el-icon-delete"
                >
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        title="查看亲子课内容"
        :visible.sync="centerDialogVisible"
        width="60%"
        center
      >
        <el-row :gutter="20">
          <el-col :span="12" style="color: #9c446e;font-size: 22px;">
            {{ row.title }}
          </el-col>
        </el-row>
        <el-row :gutter="20" style="display: flex; align-items: center;">
          <el-col :span="6" >
            封面图:
          </el-col>
          <el-col :span="6">
            <img :src="baseurl + row.imgrul" style="width: 100px;height: 100px;">
          </el-col>
        </el-row>
        <el-row :gutter="2" style="display: flex; align-items: center;">
          <el-col :span="6" >
            亲子视频:
          </el-col>
          <el-col :span="6">
            <video controls  :src="baseurl + row.videourl" style="width: 300px; height: 300px;"></video>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="centerDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>

    <!-- 抽屉数据 -->
    <div v-else>
      <spuindex :row="row"></spuindex>
    </div>
  </div>
</template>

<script>
import spuindex from "../Test1/spuform/index.vue";
import _ from "lodash";
export default {
  name: "",

  data() {
    return {
      staticshow: true,
      classdata: [],
      getreqgetclassdata: [],
      classname: "",
      cForm: {
        gradeid: "",
      },
      baseurl: "https://zyc618.mynatapp.cc",
      row: {},
      centerDialogVisible: false,
    };
  },
  methods: {
    //获取左边列表:
    async getdata() {
      let result = await this.$API.text.reqclassdata();
      if (result.code === 200) {
        //获取
        this.getreqgetclassdata = result.data.data.results;
        this.gettabledata(this.getreqgetclassdata[0].list2_leftid);
      }
    },
    //获取表格数据:
    async gettabledata(id) {
      let result = await this.$API.text.gettabledata(id);
      if (result.code == 200) {
        //获取
        this.classdata = result.data.data.results;
      }
    },
    //点击选择框后的函数:
    getgradeid() {
      this.gettabledata(this.cForm.gradeid);
    },

    //info按钮
    maintenance(row) {
      
      this.row = row;
      this.centerDialogVisible = true;
    },
    async deletebutton() {
      this.$message({
        type: "error",
        message: "权限不足,禁止删除",
      });
    },
    updatashop: _.throttle(function (row) {
      let index = this.classdata.findIndex(
        (obj) => obj.list2_rightid === row.list2_rightid
      );
      this.classdata[index].ishow = !this.classdata[index].ishow;
      //发起请求:
      this.changeishow(row);
      if (this.classdata[index].ishow) {
        this.$message({
          message: "发布视频",
          type: "success",
        });
      } else {
        this.$message({
          message: "下架视频",
          type: "error",
        });
      }
    }, 1500),
    showshopdetail(row) {
      this.row = row;
      this.staticshow = !this.staticshow;
    },
    //
    addclassdata() {
      if (this.cForm.gradeid == "") {
        this.$message({
          message: "请选择标题,再添加内容",
          type: "info",
        });
      } else {
        this.staticshow = !this.staticshow;
        this.row = {
          gradeid: this.cForm.gradeid,
          title: "",
          imgrul: "",
          videourl: "",
        };
      }
    },
    //上架或发布视频状态:
    async changeishow(row_s) {
      let result = await this.$API.text.changeishow(row_s);
    },
    debouncedInputChange: _.throttle(function () {
      // 在这里处理防抖后的逻辑，例如向服务器发送请求等
      this.$bus.$on("change_row", (data) => {
        let objToChange = this.classdata.findIndex(
          (obj) => obj.list2_rightid == data.list2_rightid
        );
        console.log(objToChange);
        if (objToChange != -1) {
          this.classdata[objToChange] = data;
        } else {
          this.classdata.push(data);
        }
      });
    }, 500),
  },
  mounted() {
    this.getdata();
    this.$bus.$on("change_staticshow", (data) => {
      this.staticshow = data;
    });
    this.debouncedInputChange();
  },
  components: {
    spuindex,
  },
};
</script>

<style scoped></style>
